import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import testvue from '@/components/testvue'
import job from '@/components/job'
import register from '@/components/register'
import login from '@/components/login'
import mypage from '@/components/mypage'

Vue.use(Router)

var routes = [
  {
    path: '/',
    name: 'index',
    component: index
  },
  {
    path: '/testvue',
    name: 'testvue',
    component: testvue
  },
  {
    path: '/job',
    name: 'job',
    component: job
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  // 使用拦截器，拦截非登录用户
  {
    path: '/mypage',
    name: 'mypage',
    component: mypage,
    beforeEnter: (to, from, next) => {
      // 判断是否登录
      if(localStorage.getItem('username')){
        console.log('已登陆');
        next();
      }
      else{
        console.log('未登录');
        // 强制跳转
        next('/login')
      }
    }
  },
]

export default new Router({
  routes: routes,
  mode: 'history'   /*hash*/
})
